home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / moni / systemviewer.lha / SysResident.c < prev    next >
C/C++ Source or Header  |  2001-04-25  |  14KB  |  613 lines

  1. /****h* SystemViewer/SysResident.c [1.1] ********************************
  2. *
  3. * NAME
  4. *    SysResident.c
  5. *
  6. * DESCRIPTION
  7. *    Show the user a GUI of the system-resident programs.
  8. *
  9. * NOTES
  10. *    GUI Designed by : Jim Steichen
  11. *************************************************************************
  12. *
  13. */
  14.  
  15. #include <string.h>
  16.  
  17. #include <exec/types.h>
  18. #include <exec/execbase.h>
  19. #include <exec/resident.h>
  20.  
  21. #include <intuition/intuition.h>
  22. #include <intuition/classes.h>
  23. #include <intuition/classusr.h>
  24. #include <intuition/gadgetclass.h>
  25.  
  26. #include <libraries/gadtools.h>
  27.  
  28. #include <graphics/displayinfo.h>
  29. #include <graphics/gfxbase.h>
  30.  
  31. #include <clib/exec_protos.h>
  32. #include <clib/intuition_protos.h>
  33. #include <clib/gadtools_protos.h>
  34. #include <clib/graphics_protos.h>
  35. #include <clib/utility_protos.h>
  36. #include <clib/diskfont_protos.h>
  37.  
  38. #include "CPGM:GlobalObjects/CommonFuncs.h"
  39.  
  40. #include "SysLists.h"
  41.  
  42. #define SLV       0
  43. #define Update    1
  44. #define AddBt     2
  45. #define RemoveBt  3
  46. #define ReplaceBt 4
  47. #define Cancel    5
  48.  
  49. #define SLVGADGET SRGadgets[ SLV ]
  50.  
  51. #define SR_CNT    6
  52.  
  53. IMPORT struct ExecBase *SysBase;
  54.  
  55. // -------------------------------------------------------------------
  56.  
  57. PRIVATE char vr[] = "\0$VER: SysResident 1.1 (25-Apr-2001) by J.T. Steichen\0";
  58.  
  59. // -------------------------------------------------------------------
  60.  
  61. PRIVATE struct TextFont     *SRFont  = NULL;
  62. PRIVATE struct Window       *SRWnd   = NULL;
  63. PRIVATE struct Gadget       *SRGList = NULL;
  64. PRIVATE struct IntuiMessage  SRMsg;
  65. PRIVATE struct Gadget       *SRGadgets[ SR_CNT ];
  66.  
  67. PRIVATE UWORD  SRLeft   = 0;
  68. PRIVATE UWORD  SRTop    = 16;
  69. PRIVATE UWORD  SRWidth  = 632;
  70. PRIVATE UWORD  SRHeight = 228;
  71. PRIVATE UBYTE *SRWdt    = (UBYTE *) "System Residents Info:";
  72.  
  73. PRIVATE UBYTE *ttl = "Address  Type     Pri   Ver  Flags     Name";
  74. PRIVATE UBYTE *fmt = "%08LX %-8.8s %4d %3d   %08LX  %-32.32s";
  75.  
  76. PRIVATE ULONG Residents = 0L; // SysBase->ResModules.
  77.  
  78. // -------------------------------------------------------------------
  79.  
  80. #define NODELENGTH 80
  81.  
  82. PRIVATE struct List         SRList     = { 0, };
  83. PRIVATE struct Node        *SRNodes    = NULL;
  84. PRIVATE struct ListViewMem *Reslvm     = NULL; //{ 0, 0, 0, NODELENGTH };
  85.  
  86. PRIVATE UBYTE              *NodeStrs   = NULL;
  87.  
  88. // -------------------------------------------------------------------
  89.  
  90. PRIVATE UWORD SRGTypes[] = {
  91.  
  92.    LISTVIEW_KIND, BUTTON_KIND, BUTTON_KIND,
  93.    BUTTON_KIND,   BUTTON_KIND, BUTTON_KIND,
  94. };
  95.  
  96. PRIVATE int SLVClicked(       int whichitem );
  97. PRIVATE int UpdateClicked(    int dummy     );
  98. PRIVATE int CancelClicked(    int dummy     );
  99. PRIVATE int AddBtClicked(     int dummy     );
  100. PRIVATE int RemoveBtClicked(  int dummy     );
  101. PRIVATE int ReplaceBtClicked( int dummy     );
  102.  
  103. PRIVATE struct NewGadget SRNGad[] = {
  104.  
  105.      2,   3, 627, 200,                NULL, NULL, SLV,               0, 
  106.    NULL, (APTR) SLVClicked,
  107.  
  108.      4, 205,  72,  17, (UBYTE *) "_Update", NULL, Update, PLACETEXT_IN, 
  109.    NULL, (APTR) UpdateClicked,
  110.  
  111.     80, 205,  72,  17, (UBYTE *) "Add", NULL, AddBt, PLACETEXT_IN, 
  112.    NULL, (APTR) AddBtClicked,
  113.  
  114.    155, 205,  72,  17, (UBYTE *) "Remove", NULL, RemoveBt, PLACETEXT_IN, 
  115.    NULL, (APTR) RemoveBtClicked,
  116.  
  117.    230, 205,  72,  17, (UBYTE *) "Replace", NULL, ReplaceBt, PLACETEXT_IN, 
  118.    NULL, (APTR) ReplaceBtClicked,
  119.  
  120.    554, 205,  72,  17, (UBYTE *) "_Cancel", NULL, Cancel, PLACETEXT_IN, 
  121.    NULL, (APTR) CancelClicked
  122. };
  123.  
  124. PRIVATE ULONG SRGTags[] = {
  125.  
  126. //   GTLV_ReadOnly,     TRUE, 
  127.    GTLV_ShowSelected, NULL, GTLV_Selected, 1,
  128.    LAYOUTA_Spacing,   2, 
  129.    TAG_DONE,
  130.    
  131.    GT_Underscore, '_', TAG_DONE,
  132.    TAG_DONE,
  133.    TAG_DONE,
  134.    TAG_DONE,
  135.    GT_Underscore, '_', TAG_DONE
  136. };
  137.  
  138. // -------------------------------------------------------------------
  139.  
  140. PRIVATE void SetupLV( struct List *list, struct ListViewMem *lvm )
  141. {
  142.    SetupList( list, lvm ); 
  143.    
  144.    strcpy( lvm->lvm_Nodes[0].ln_Name, ttl );
  145.  
  146.    return;
  147. }
  148.  
  149. PRIVATE int InitializeLV( int numitems )
  150. {
  151.    if ((Reslvm = Guarded_AllocLV( numitems, NODELENGTH )) == NULL)
  152.       {
  153.       ReportAllocLVError();
  154.  
  155.       return( -1 );
  156.       }
  157.  
  158.    NodeStrs = Reslvm->lvm_NodeStrs;
  159.    SRNodes  = Reslvm->lvm_Nodes;
  160.  
  161.    SetupLV( &SRList, Reslvm );
  162.  
  163.    return( 0 );
  164. }
  165.  
  166. PRIVATE int CountResidents( void )
  167. {
  168.    char **pointer = 0L;
  169.    int    rval    = 0;
  170.    
  171.    Forbid();
  172.    
  173.       if (Residents == NULL)
  174.          Residents = (ULONG *) SysBase->ResModules;
  175.       
  176.       pointer = (char **) Residents;
  177.  
  178.       while (*pointer != NULL) // pointer->rt_Name != NULL???? 
  179.          {
  180.          pointer++;
  181.          rval++;
  182.          }      
  183.    
  184.    Permit();
  185.    
  186.    return( rval + 2 ); // add some padding.
  187. }
  188.  
  189. PRIVATE char nt[10] = "", *nodeType = &nt[0];
  190.  
  191. PRIVATE char *GetType( UBYTE nodetype )
  192. {
  193.    switch (nodetype)
  194.       {
  195.       case NT_LIBRARY:
  196.          strcpy( nodeType, "Library" );
  197.          break;
  198.  
  199.       case NT_RESOURCE:
  200.          strcpy( nodeType, "Resource" );
  201.          break;
  202.  
  203.       case NT_DEVICE:
  204.          strcpy( nodeType, "Device" );
  205.          break;
  206.  
  207.       case NT_TASK:
  208.          strcpy( nodeType, "Task" );
  209.          break;
  210.  
  211.       default:
  212.          strcpy( nodeType, "Unknown" );
  213.          break;
  214.       }
  215.       
  216.    return( nodeType );
  217. }
  218.  
  219. PRIVATE void MakeResidentList( void )
  220. {
  221.    char **resident = 0L;
  222.    int    i;
  223.  
  224.    DisplayTitle( SRWnd, "Making list of Residents..." );
  225.    
  226.    HideListFromView( SLVGADGET, SRWnd );
  227.  
  228.    Forbid();
  229.       resident = (char **) SysBase->ResModules;
  230.       
  231.       for (i = 1; (*resident != NULL) && (i < Reslvm->lvm_NumItems); i++)
  232.          {
  233.          //"Address  Type     Pri  Ver  Flags     Name"
  234.          sprintf( &NodeStrs[ i * NODELENGTH ], fmt,
  235.                   *resident, 
  236.  
  237.                   GetType( ((struct Resident *) *resident)->rt_Type ), 
  238.  
  239.                   ((struct Resident *) *resident)->rt_Pri,
  240.                   ((struct Resident *) *resident)->rt_Version,
  241.                   ((struct Resident *) *resident)->rt_Flags,
  242.                   ((struct Resident *) *resident)->rt_Name
  243.                 );
  244.       
  245.          resident++;
  246.          }
  247.  
  248.    Permit();
  249.  
  250.    ModifyListView( SLVGADGET, SRWnd, &SRList, NULL );
  251.  
  252.    DisplayTitle( SRWnd, SRWdt );
  253.  
  254.    return;
  255. }
  256.  
  257. // -------------------------------------------------------------------
  258.  
  259. PRIVATE BOOL  ResSelected  = FALSE;
  260. PRIVATE ULONG SelectedAddr = 0L;
  261.  
  262. PRIVATE int SLVClicked( int whichitem )
  263. {
  264.    if (whichitem == 0)
  265.       {
  266.       ResSelected = FALSE;
  267.       return( TRUE );  // Dopey User selected the column titles!
  268.       }
  269.    else
  270.       {
  271.       char t[256];
  272.       long addr = 0L;
  273.       
  274.       strcpy( t, &Reslvm->lvm_NodeStrs[ whichitem * NODELENGTH ] );
  275.       
  276.       (void) stch_l( t, &addr );
  277.       
  278.       sprintf( &t[0], "%s - you selected: 0x%08LX", SRWdt, addr );
  279.  
  280.       DisplayTitle( SRWnd, &t[0] );
  281.  
  282.       ResSelected  = TRUE;
  283.       SelectedAddr = (ULONG) addr;
  284.       }
  285.  
  286.    return( TRUE );
  287. }
  288.  
  289. PRIVATE void CloseSRWindow( void );
  290.  
  291. PRIVATE int UpdateClicked( int dummy )
  292. {
  293.    int count = CountResidents();
  294.    
  295.    if (count > Reslvm->lvm_NumItems)
  296.       {
  297.       // Deallocate old memory & reallocate for count variable:
  298.       Guarded_FreeLV( Reslvm );
  299.  
  300.       if (InitializeLV( count ) < 0)
  301.          {
  302.          sprintf( ErrMsg, "Couldn't get more memory for:\n"
  303.                           "   SysResidents Requester!" 
  304.                 );
  305.  
  306.          UserInfo( ErrMsg, "Allocation Problem:" );
  307.   
  308.          CloseSRWindow();
  309.  
  310.          return( FALSE );
  311.          }
  312.       }
  313.       
  314.    MakeResidentList();
  315.    
  316.    return( TRUE );
  317. }
  318.  
  319. PRIVATE int AddBtClicked( int dummy )
  320. {
  321.    // Show the user a string requester:   
  322.    UserInfo( "Not implemented yet!", "USER Information:" );
  323.  
  324.    return( TRUE );
  325. }
  326.  
  327. PRIVATE int RemoveBtClicked( int dummy )
  328. {
  329.    if (ResSelected == FALSE)
  330.       {
  331.       strcpy( ErrMsg, "Select a Resident to remove first!" );
  332.  
  333.       UserInfo( ErrMsg, "User ERROR:" );
  334.       return( TRUE );
  335.       }
  336.    else
  337.       {
  338.       UserInfo( "Not implemented yet!", "USER Information:" );
  339.       return( TRUE );
  340.  
  341. //      char cmd[256];
  342. //      int  ans = 0;
  343.        
  344.       // Give the user a last chance to bail out:
  345.       
  346. //      ans = Handle_Problem();
  347.                    
  348. //      ((struct Resident *) SelectedAddr)->rt_Name
  349.       }
  350. }
  351.  
  352. PRIVATE int ReplaceBtClicked( int dummy )
  353. {
  354.    // Show the user a string requester:   
  355.  
  356.    UserInfo( "Not implemented yet!", "USER information:" );   
  357.  
  358.    return( TRUE );
  359. }
  360.  
  361. // -------------------------------------------------------------------
  362.  
  363. PRIVATE void CloseSRWindow( void )
  364. {
  365.    if (SRWnd != NULL)
  366.       {
  367.       CloseWindow( SRWnd );
  368.       SRWnd = NULL;
  369.       }
  370.  
  371.    if (SRGList != NULL)
  372.       {
  373.       FreeGadgets( SRGList );
  374.       SRGList = NULL;
  375.       }
  376.  
  377.    if (SRFont != NULL)
  378.       {
  379.       CloseFont( SRFont );
  380.       SRFont = NULL;
  381.       }
  382.  
  383.    return;
  384. }
  385.  
  386. PRIVATE int SRCloseWindow( void )
  387. {
  388.    CloseSRWindow();
  389.    return( FALSE );
  390. }
  391.  
  392. PRIVATE int CancelClicked( int dummy )
  393. {
  394.    return( SRCloseWindow() );
  395. }
  396.  
  397. // -------------------------------------------------------------------
  398.  
  399. PRIVATE int OpenSRWindow( void )
  400. {
  401.    struct NewGadget  ng;
  402.    struct Gadget    *g;
  403.    UWORD             lc, tc;
  404.    UWORD             wleft = SRLeft, wtop = SRTop, ww, wh;
  405.  
  406.    ComputeFont( Scr, Font, &CFont, SRWidth, SRHeight );
  407.  
  408.    ww = ComputeX( CFont.FontX, SRWidth );
  409.    wh = ComputeY( CFont.FontY, SRHeight );
  410.  
  411.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width)
  412.       wleft = Scr->Width - ww;
  413.  
  414.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height)
  415.       wtop = Scr->Height - wh;
  416.  
  417.    if ((SRFont = OpenDiskFont( Font )) == NULL)
  418.       return( -5 );
  419.  
  420.    if ((g = CreateContext( &SRGList )) == NULL)
  421.       return( -1 );
  422.  
  423.    for (lc = 0, tc = 0; lc < SR_CNT; lc++)
  424.       {
  425.       CopyMem( (char *) &SRNGad[lc], (char *) &ng, 
  426.                (long) sizeof( struct NewGadget )
  427.              );
  428.  
  429.       ng.ng_VisualInfo = VisualInfo;
  430.       ng.ng_TextAttr   = Font;
  431.  
  432.       ng.ng_LeftEdge   = CFont.OffX + ComputeX( CFont.FontX, 
  433.                                                 ng.ng_LeftEdge
  434.                                               );
  435.  
  436.       ng.ng_TopEdge    = CFont.OffY + ComputeY( CFont.FontY,
  437.                                                 ng.ng_TopEdge
  438.                                               );
  439.  
  440.       ng.ng_Width      = ComputeX( CFont.FontX, ng.ng_Width );
  441.       ng.ng_Height     = ComputeY( CFont.FontY, ng.ng_Height );
  442.  
  443.       SRGadgets[lc] = g 
  444.                     = CreateGadgetA( (ULONG) SRGTypes[lc], 
  445.                                      g, 
  446.                                      &ng, 
  447.                                      (struct TagItem *) &SRGTags[tc]
  448.                                    );
  449.  
  450.       while (SRGTags[tc] != TAG_DONE)
  451.          tc += 2;
  452.  
  453.       tc++;
  454.  
  455.       if (g == NULL)
  456.          return( -2 );
  457.       }
  458.  
  459.    if ((SRWnd = OpenWindowTags( NULL,
  460.  
  461.                   WA_Left,    wleft,
  462.                   WA_Top,     wtop,
  463.                   WA_Width,   ww + CFont.OffX + Scr->WBorRight,
  464.                   WA_Height,  wh + CFont.OffY + Scr->WBorBottom,
  465.                   
  466.                   WA_IDCMP,   LISTVIEWIDCMP | BUTTONIDCMP 
  467.                     | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW 
  468.                     | IDCMP_VANILLAKEY,
  469.                   
  470.                   WA_Flags,   WFLG_DRAGBAR | WFLG_DEPTHGADGET 
  471.                     | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH 
  472.                     | WFLG_ACTIVATE | WFLG_RMBTRAP,
  473.  
  474.                   WA_Gadgets, SRGList,
  475.                   WA_Title,   SRWdt,
  476.                   TAG_DONE )
  477.       ) == NULL)
  478.       return( -4 );
  479.  
  480.    GT_RefreshWindow( SRWnd, NULL );
  481.  
  482.    return( 0 );
  483. }
  484.  
  485. PRIVATE int SRVanillaKey( int whichkey )
  486. {
  487.    int rval = TRUE;
  488.    
  489.    switch (whichkey)
  490.       {
  491.       case 'c':
  492.       case 'C':
  493.       case 'q':
  494.       case 'Q':
  495.       case 'x':
  496.       case 'X':
  497.          rval = CancelClicked( 0 );
  498.          break;
  499.          
  500.       case 'u':
  501.       case 'U':
  502.          rval = UpdateClicked( 0 );
  503.          break;
  504.       }
  505.       
  506.    return( rval );
  507. }
  508.  
  509. PRIVATE int HandleSRIDCMP( void )
  510. {
  511.    struct IntuiMessage  *m;
  512.    int                 (*func)( int code );
  513.    BOOL                  running = TRUE;
  514.  
  515.    while (running == TRUE)
  516.       {
  517.       if ((m = GT_GetIMsg( SRWnd->UserPort )) == NULL)
  518.          {
  519.          (void) Wait( 1L << SRWnd->UserPort->mp_SigBit );
  520.          continue;
  521.          }
  522.  
  523.       CopyMem( (char *) m, (char *) &SRMsg, 
  524.                (long) sizeof( struct IntuiMessage )
  525.              );
  526.  
  527.       GT_ReplyIMsg( m );
  528.  
  529.       switch (SRMsg.Class)
  530.          {
  531.          case IDCMP_REFRESHWINDOW:
  532.             GT_BeginRefresh( SRWnd );
  533.             GT_EndRefresh( SRWnd, TRUE );
  534.             break;
  535.  
  536.          case IDCMP_CLOSEWINDOW:
  537.             running = SRCloseWindow();
  538.             break;
  539.  
  540.          case IDCMP_VANILLAKEY:
  541.             running = SRVanillaKey( SRMsg.Code );
  542.             break;
  543.             
  544.          case IDCMP_GADGETUP:
  545.          case IDCMP_GADGETDOWN:
  546.             func = (void *) ((struct Gadget *) SRMsg.IAddress)->UserData;
  547.             
  548.             if (func != NULL)
  549.                running = func( SRMsg.Code );
  550.             break;
  551.          }
  552.       }
  553.  
  554.    return( running );
  555. }
  556.  
  557. PUBLIC int SystemResidents( void )
  558. {
  559.    int numitems = 0;
  560.  
  561.    if (SetupSystemList( &OpenSRWindow ) < 0)
  562.       {
  563.       fprintf( stderr, "Couldn't open a System ListViewer!\n" );
  564.       return( RETURN_FAIL );
  565.       }
  566.  
  567.    SetNotifyWindow( SRWnd );
  568.          
  569.    Forbid();
  570.       Residents = SysBase->ResModules;
  571.    Permit();
  572.  
  573.    numitems = CountResidents();
  574.  
  575.    if (InitializeLV( numitems ) < 0)
  576.       {
  577.       sprintf( ErrMsg, "Couldn't get memory for:\n"
  578.                        "   SysResidents Requester!" 
  579.              );
  580.  
  581.       UserInfo( ErrMsg, "Allocation Problem:" );
  582.  
  583.       CloseSRWindow();
  584.  
  585.       return( RETURN_FAIL );
  586.       }
  587.  
  588.    MakeResidentList();
  589.       
  590.    GT_RefreshWindow( SRWnd, NULL );
  591.  
  592.    (void) HandleSRIDCMP();
  593.  
  594.    Guarded_FreeLV( Reslvm );
  595.  
  596.    CloseSRWindow();         // Just in case.   
  597.  
  598.    ShutdownSystemList();
  599.       
  600.    return( RETURN_OK );
  601. }
  602.  
  603. #ifdef DEBUG
  604.  
  605. PUBLIC int main( void )
  606. {
  607.    return( SystemResidents() );
  608. }
  609.  
  610. #endif
  611.  
  612. /* ----------------- END of SysResident.c file! ------------------------ */
  613.